package com.lvtongka.app.fragment.home.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.lvtongka.app.R
import com.lvtongka.app.databinding.HomeOfflineItemBinding
import com.lvtongka.app.fragment.home.bean.OfflineItem

class HomeOfflineAdapter(private val itemClickListener: ItemClickListener) : RecyclerView.Adapter<HomeOfflineAdapter.BaseHomeHolder>() {
    private var mList: MutableList<OfflineItem> = mutableListOf()

    @SuppressLint("NotifyDataSetChanged")
    fun setList(list: List<OfflineItem>) {
        mList.clear()
        mList.addAll(list)
        notifyDataSetChanged()
    }

    open inner class BaseHomeHolder(val binding: HomeOfflineItemBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseHomeHolder {
        var binding = HomeOfflineItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return BaseHomeHolder(binding)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: BaseHomeHolder, position: Int) {
        val data = mList[position]
        holder.binding.tvItem.text = data.name
        if (data.selectItem) {
            holder.binding.tvItem.setBackgroundResource(R.drawable.bg_btn_pink)
            holder.binding.tvItem.setTextColor(holder.binding.tvItem.resources.getColor(R.color.textBD5557))
        } else {
            holder.binding.tvItem.setBackgroundResource(R.drawable.bg_btn_white)
            holder.binding.tvItem.setTextColor(holder.binding.tvItem.resources.getColor(R.color.black))
        }
        holder.binding.tvItem.setOnClickListener {
            itemClickListener.onItemClick(data)
        }
    }

    interface ItemClickListener{
        fun onItemClick(item: OfflineItem)
    }
}